package cn.qinggu.cloud.web.common;

import org.springframework.ui.ModelMap;

/**
 * Created by shk
 * 2017/4/26 19:35
 */
public class ResponseModel extends ModelMap {

    private final String CODE = "code";
    private final String MESSAGE = "message";
    private final String DATA = "data";


    public ResponseModel() {
    }

    /**
     * 初始化模型
     *
     * @param isSuccess 是否初始化为成功，true：初始化为成功，false：初始化为失败
     */
    public ResponseModel(boolean isSuccess) {
        super();
        if (isSuccess) {
            setSuccess();
        } else {
            setError();
        }
    }

    /**
     * 设置操作成功
     */
    public void setSuccess() {
        put(CODE, CodeConstant.CODE_SUCCESS);
        put(MESSAGE, CodeConstant.CODE_SUCCESS_MESSAGE);
    }

    /**
     * 设置操作失败
     */
    public void setError() {
        put(CODE, CodeConstant.CODE_ERROR);
        put(MESSAGE, CodeConstant.CODE_ERROR_MESSAGE);
    }

    /**
     * 设置操作失败
     *
     * @param message 操作失败时的提示语
     */
    public void setError(String message) {
        put(CODE, CodeConstant.CODE_ERROR);
        put(message, message);
    }

    public void setData(Object data) {
        put(DATA, data);
    }

    public void init(String code, String message) {
        put(CODE, code);
        put(MESSAGE, message);
    }

}
